package com.woniu.controller;

import com.woniu.entity.Course;
import com.woniu.entity.Order;
import com.woniu.entity.PageResult;
import com.woniu.entity.dto.OrderAddsc;
import com.woniu.entity.dto.OrderCourseDTO;

import com.woniu.service.OrderService;
import com.woniu.utils.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/orders")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/getOrders")
    public Map<String, Object> getOrders(
            @RequestParam(value = "orderMethod", required = false) String orderMethod,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "userId", required = false) Integer userId,
            @RequestParam(value = "username", required = false) String username,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize) {
        PageResult<Order> pageInfo = orderService.getOrdersByPage(orderMethod, status, userId, username, pageNum, pageSize);
        System.out.println("------->" + pageInfo);
        Map<String, Object> result = new HashMap<>();
        result.put("pageNum", pageInfo.getPageNum());
        result.put("pageSize", pageInfo.getPageSize());
        result.put("total", pageInfo.getTotal());
        result.put("pages", pageInfo.getPages());
        result.put("data", pageInfo.getList());

        return result;
    }

    @DeleteMapping("/deleteOrder/{orderId}")
    public ResultObj deleteOrder(@PathVariable String orderId) {
        orderService.deleteOrder(orderId);
        orderService.deleteOrderAndCourse(orderId);
        return ResultObj.ok();
    }

    //更新备注
    @PostMapping("/updateRemark")
    public Map<String, Object> updateRemark(@RequestBody Map<String, Object> params) {
        String orderId = (String) params.get("orderId");
        String remark = (String) params.get("remark");

        orderService.updateRemark(orderId, remark);

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "备注更新成功");
        return result;
    }

    //获取订单详情
    @GetMapping("/detail/{orderId}")
    public Map<String, Object> getOrderDetail(@PathVariable String orderId) {
        Order order = orderService.getOrderById(orderId);

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", order);
        return result;
    }

    /**
     * 创建多个订单
     *
     * @param Orders 物品ID列表
     * @return 总订单编号
     */
    @PostMapping("/createBatch")
    public String insertOrders(@RequestBody List<Order> Orders) {
        return orderService.createOrders(Orders);
    }


    /**
     * \查询所有购买课程
     * @param
     * @return
     */
    @PostMapping("/selectaaaa")
    public ResultObj select(@RequestBody OrderAddsc sc) {
        System.out.println("sssssssssssss");
        System.out.println(sc.getUserId());
        List<Course> courses = orderService.selectOrder(sc.getUserId());
        return ResultObj.ok(courses);
    }

//    /**
//     * 查询订单
//     * @param orderNo 总订单编号
//     * @return 订单列表
//     */
//    @GetMapping("/selectOrders")
//    public List<Order> selectOrders(@RequestParam String orderNo) {
//        return orderService.getOrdersByOrderNo(orderNo);
//    }
//
//    /**
//     * 支付订单
//     * @param orderNo 总订单编号
//     * @return 支付结果
//     */
//    @PostMapping("/payOrders")
//    public boolean payOrders(@RequestParam String orderNo) {
//        return orderService.payOrders(orderNo);
//    }
//
//    /**
//     * 创建一个订单
//     */
//    @PostMapping("/create")
//    public ResultObj createOrder(@RequestBody OrderDTO orderDTO) {
//        Order order = orderService.createOrder(orderDTO);
//        return ResultObj.ok(order);
//    }
/**
 * 查询是否购买当前课程
 */
@PostMapping("/selectxx")
public ResultObj selectxx(@RequestBody OrderAddsc sc) {

    Integer  a =orderService.selectUC(sc.getUserId(),sc.getCourseId());
    if (a==null){
        return ResultObj.ok(0);
    }
        return ResultObj.ok(1);
}

    /**
     * 支付榜
     */
    @GetMapping("/paymentRankings")
    public ResultObj<List<Map<String, Object>>> getPaymentRankings() {
        List<Map<String, Object>> rankings = orderService.getPaymentRankings();
        return ResultObj.ok(rankings);
    }

    // 获取订单详情及关联课程
    @GetMapping("/detailWithCourses/{orderId}")
    public ResultObj<OrderCourseDTO> getOrderDetailWithCourses(@PathVariable String orderId) {
        Order order = orderService.getOrderById(orderId);
        List<Course> courses = orderService.getCoursesByOrderId(orderId);

        OrderCourseDTO dto = new OrderCourseDTO();
        dto.setOrder(order);
        dto.setCourses(courses);

        return ResultObj.ok(dto);
    }

    // 获取课程详情及用户信息
    @GetMapping("/courseDetail/{courseId}")
    public ResultObj<Map<String, Object>> getCourseDetailWithUser(@PathVariable Long courseId) {
        Map<String, Object> result = orderService.getCourseDetailWithUser(courseId);
        return ResultObj.ok(result);
    }

    @PostMapping("selectSC")
    public ResultObj getOrders(@RequestBody OrderAddsc sc) {
        //            判断是否收藏
        Integer a = orderService.selectUidByCid(sc.getUserId(), sc.getCourseId());
        if (a == null) {
            a = 0;
        } else {
            a = 1;
        }
        System.out.println("收藏状态" + a);
        return ResultObj.ok(a);
    }

    @PostMapping("/addsc")
    public ResultObj addsc(@RequestBody OrderAddsc sc) {
        System.out.println("getA" + sc.getA());
        Integer a = orderService.selectUidByCid(sc.getUserId(), sc.getCourseId());

        try {
            if (a != null) {
//                删除收藏
                orderService.delectsc(sc.getUserId(), sc.getCourseId());
                System.out.println("取消收藏成功");
                return ResultObj.ok(1);
            }
            if (a == null) {
                //添加收藏
                orderService.addsc(sc.getUserId(), sc.getCourseId());
                System.out.println("收藏成功");
                return ResultObj.ok(0);
            }
            return ResultObj.ok(3);
        } catch (Exception e) {
            return ResultObj.ok(2);
        }
    }
}
